package cn.org.timespace.easyframe.util.interfaceSet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.codehaus.xfire.client.Client;

public class InterfaceClient {
    
    private URL url;
    private HttpURLConnection urlconn;
    String inencoding;
    String outencoding;
    
    public InterfaceClient(String inencoding, String outencoding) {
        this.inencoding = inencoding;
        this.outencoding = outencoding;
    }
    
    /**
     * 用HTTP方式调用网络资源
     * params  格式：  c_id=10041&cpid=5&c_type=1&lotid=1&expect=08059,最好用utf-8的字符集，虽然在代码中我做了自动转化，但为了防止会有乱码
     * postUrl 格式：   http://IP:端口/requestwap.php?
     * 返回值： String
     * */
    public String httpConnect(String postUrl,String params) {
        BufferedReader br = null;
        String response = "", brLine = "";
        try {
            params=URLEncoder.encode(params,"UTF-8"); //use URLEncoder.encode for encode the params

            url = new URL(postUrl);
            urlconn = (HttpURLConnection) url.openConnection();
            urlconn.setRequestProperty("user-agent","mozilla/4.7 [en] (win98; i)");    //set request header 
            urlconn.setRequestProperty("X-Forwarded-For", "127.0.0.1");
            urlconn.setConnectTimeout(30000);
            urlconn.setReadTimeout(30000);
            urlconn.setRequestMethod("POST");     // request method, default GET
            urlconn.setUseCaches(false);    //Post can not user cache
            urlconn.setDoOutput(true);    //set output from urlconn
            urlconn.setDoInput(true);    //set input from urlconn
            OutputStream out = urlconn.getOutputStream();
            out.write(params.getBytes(outencoding));
            out.flush();
            out.close();    // output stream close,That's means need not to post data to this outputstream

            br = new BufferedReader(new InputStreamReader(urlconn.getInputStream(), inencoding));
            while((brLine = br.readLine())!=null)
                response =(new StringBuilder(String.valueOf(response))).append(brLine).toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(br != null) {
                    br.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            urlconn.disconnect();
        }
        return response;
    }
    
    /**
     * 用WebService方式调用网络资源
     * params  格式：  a###b###c###d,参数和参数之间用“###”来分隔
     * postUrl 格式：   http://192.168.2.27:88/Work/services/SmsWebService?wsdl
     * 返回值： String
     * */
    public String webServiceConnect(String postUrl,String method,String params){
    	String resultStr="";
    	try {
			Client client = new Client(new URL(postUrl)); // 根据WSDL创建客户实例
			Object[] infrom=params.split("###");
			Object[] results = client.invoke(method,infrom);
			
			resultStr=(String)results[0];
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultStr;
    }
}
